package cn.xfdzx.android.apps.shop.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.myshop.OrderDetailPringActivity;
import cn.xfdzx.android.apps.shop.bean.ShopOrderListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.ShopInGoodListPresenter;
import cn.xfdzx.android.apps.shop.util.Utils;

/**
 * @author gzk
 * @date 2020/5/18 0018
 * 商家入驻  订单管理的列表
 * description：
 */
public class ShopInOrderListAdapter extends RecyclerView.Adapter {


    private List<ShopOrderListBeen.DataBean> mList;
    private ItemClickListener itemClickListener;


    private Context mContext;
    private final LayoutInflater mLayoutInflater;
    private ShopInGoodListPresenter mPresenter;
    private String styleType;
    private ItemClickListener clickListener;


    public ShopInOrderListAdapter(Context mContext, List<ShopOrderListBeen.DataBean> list, String styleType, ItemClickListener itemClickListener) {
        this.mContext = mContext;
        this.mList = list;
        this.clickListener = itemClickListener;

        this.styleType = styleType;

        //以后用它来初始化布局
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        return new ShopInOrderListAdapter.OrderViewHolder0(mContext, mLayoutInflater.inflate(R.layout.item_shop_in_order_list, null));


    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        ShopInOrderListAdapter.OrderViewHolder0 orderViewHolder0 = (ShopInOrderListAdapter.OrderViewHolder0) holder;
        orderViewHolder0.setData(position);


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, OrderDetailPringActivity.class);
                boolean  is;
                if (!styleType.equals("0") && mList.get(position).getDelivery_method() == 2 && (mList.get(position).getStatus() == 1|| mList.get(position).getStatus() == 0)) {
                   is=true;
                }else {
                    is=false;
                }
                intent.putExtra("is_ti",is);
                intent.putExtra("orderId", mList.get(position).getOrder_id());
                mContext.startActivity(intent);
            }
        });
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }


    public class OrderViewHolder0 extends RecyclerView.ViewHolder {
        //        @BindView(R.id.iv_good_image)
//        ImageView ivGoodImage;
        @BindView(R.id.tv_order_num)
        TextView tvOrderNum;
        @BindView(R.id.tv_order_state)
        TextView tvOrderState;
        @BindView(R.id.rv_good_list)
        RecyclerView rvGoodList;
        @BindView(R.id.tv_order_time)
        TextView tvOrderTime;
        @BindView(R.id.tv_order_pay)
        TextView tvOrderPay;
        @BindView(R.id.tv_ziti)
        TextView tvZiti;
        @BindView(R.id.viewbg)
        View viewbg;


        private Context mContext;

        public OrderViewHolder0(Context mContext, View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.mContext = mContext;

        }

        public void setData(int position) {

            if (styleType.equals("0")) {
                tvOrderState.setTextColor(mContext.getResources().getColor(R.color.color_FB9129));
            } else {
                tvOrderState.setTextColor(mContext.getResources().getColor(R.color.color_02C557));
            }

            tvOrderNum.setText("订单号:" + mList.get(position).getOrder_id());
            if (!styleType.equals("0") && mList.get(position).getDelivery_method() == 2 && (mList.get(position).getStatus() == 1|| mList.get(position).getStatus() == 0)) {
                tvZiti.setVisibility(View.VISIBLE);
                tvOrderState.setText("待自提");
            } else {
                tvZiti.setVisibility(View.GONE);
                tvOrderState.setText(mList.get(position).get_status().get_title());
            }


            if (mList.get(position).getPay_time() == 0) {
                tvOrderTime.setText("");
            } else {
                tvOrderTime.setText("支付时间:" + Utils.timeStamp2Date(mList.get(position).getPay_time() + "", null));
            }
            tvOrderPay.setText("实付款:" + mList.get(position).getTotal_price());

            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            rvGoodList.setLayoutManager(linearLayoutManager);
            rvGoodList.setItemAnimator(new DefaultItemAnimator());
            ShopInOrderGoodListAdapter goodListAdapter = new ShopInOrderGoodListAdapter(mContext, mList.get(position).getCartInfo());

            rvGoodList.setAdapter(goodListAdapter);

//            tvGoodOldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);

            tvZiti.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickListener.onItemClick(position);
                }
            });
            viewbg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, OrderDetailPringActivity.class);
                    boolean  is;
                    if (!styleType.equals("0") && mList.get(position).getDelivery_method() == 2 && (mList.get(position).getStatus() == 1|| mList.get(position).getStatus() == 0)) {
                        is=true;
                    }else {
                        is=false;
                    }
                    intent.putExtra("is_ti",is);
                    intent.putExtra("orderId", mList.get(position).getOrder_id());
                    mContext.startActivity(intent);
                }
            });
        }
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }
}
